<?php


namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Admin1Controller extends Controller
{
//
    public function getAllmajor(){
        $res=  DB::table('direction')->get();
        if (!count($res)){
            return json_encode(['error_code'=>2,'msg'=>'暂无数据','data'=>$res]);
        }
        return json_encode(['error_code'=>0,'msg'=>'获取成功','data'=>$res]);
    }

    public function removeMajor(Request $request){
        $where=['dir_id'=>$request->input('dir_id')];
        foreach ($where as $key=>$value){
            if ($value==null){
                return json_encode(['error_code'=>1,'msg'=>'缺少参数'.$key,'data'=>$value]);
            }
        }
        $res=DB::table('direction')->where($where)->delete();
        if (!$res){
            return json_encode(['error_code'=>2,'msg'=>'删除失败 找不到记录','data'=>$res]);
        }
        return json_encode(['error_code'=>0,'msg'=>'删除成功','data'=>$res]);
    }

//    更新或新增 专业
    public function changeMajor(Request $request){
        $dir_name=$request->input('dir_name');
        if (!$dir_name){
            return json_encode(['error_code'=>1,'msg'=>'缺少参数 dir_name','data'=>$dir_name]);
        }
        $where=['dir_name'=>$dir_name];
        $data=[
            'dir_tchName'=>$request->input('dir_tchName'),
            'dir_coverUrl'=>$request->input('dir_coverUrl'),
            'dir_videoUrl'=>$request->input('dir_videoUrl'),
            'dir_Info'=>$request->input('dir_Info'),
        ];
        $updateData=[];
        foreach ($data as $key=>$value){
            if ($value){
                $updateData[$key]=$value;
            }
        }
        $res=DB::table('direction')->updateOrInsert($where,$updateData);
        if (!$res){
            return json_encode(['error_code'=>3,'msg'=>'修改或插入失败','data'=>$res]);
        }
        return json_encode(['error_code'=>0,'msg'=>'成功',
            'data'=>DB::table('direction')->where($where)->first()]);
    }


}
